<template>
  <div class="container">
    <div class="app-container">
      <h2>组织架构</h2>
      <el-tree
        :data="depts"
        :props="defaultProps"
        :default-expanded-keys="[1]"
        @node-click="handleNodeClick"
      >
        <!-- :expand-on-click-node="false" -->
        <template slot-scope="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- $event 实参 表示类型 -->
              <el-dropdown @command="hClick($event, data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>

        </template>
      </el-tree>

    </div>
    <!-- :id="id" -->
    <AddDapts
      ref="dept"
      :is-edit="isEdit"
      :cur-node-id="curNodeId"
      :show-dialog.sync="showDialog"
      @updateManager="getDepartment"
    />
    <!-- {{ depts.name }} -->
    <!--  -->

  </div>
</template>

<script>
import { deldepartment, getdepartment } from '@/api/department'
import { arrTree } from '@/utils'
import AddDapts from './components/adddepts.vue'

export default {
  components: { AddDapts },

  data() {
    return {
      curNodeId: '',
      showDialog: false,
      isEdit: false,
      // id: '',
      depts: [{
        name: '传智教育',
        children: [
          { name: '总裁办' },
          { name: '行政部' },
          { name: '财务部' }
        ]
      }],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async hClick(type, id) {
      this.curNodeId = id
      // this.id = id
      // console.log(this.curNodeId)
      if (type === 'add') {
        this.showDialog = true
        this.isEdit = false
      }
      if (type === 'del') {
        await deldepartment(id)
        this.$message.success('删除部门成功')
        this.getDepartment()
      }
      if (type === 'edit') {
        this.showDialog = true
        this.isEdit = true
        this.$nextTick(() => this.$refs.dept.toSava())

        // this.getDepartment()
      }
      // console.log(type)
      // console.log(this.id, 22222222)
    },

    async getDepartment() {
      const res = await getdepartment()
      this.depts = arrTree(res.data, 1)
      // console.log(res)
    },
    handleNodeClick(data) {
      // console.log(data)
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  width: 50px;
  display: inline-block;
  margin: 10px;
}
</style>
